import { defineStore } from 'pinia';

export const useStore = defineStore('user', {
  state: (): {
    name: string;
    age: number;
  } => ({
    name: '',
    age: 0
  }),
  getters: {
    getUserName: state => state.name
  },
  actions: {
    setUserName(setName: string) {
      this.name = setName;
    }
  },
  // pinia数据持久化
  persist: {
    storage: sessionStorage, // 自定义存储方式
    pick: ['name'], // 只持久化 name
    serializer: {
      serialize: value => JSON.stringify(value),
      deserialize: value => JSON.parse(value)
    }
  }
});
